package com.ymatou.iapi.sellerproduct.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ActivityProductSaleBean;
import com.ymatou.iapi.sellerproduct.service.ActivityProductStartSaleCall;
import com.ymatou.iapi.sellerproduct.service.BatchDeleteProductsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

/**
*批量删除商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BatchDeleteProducts{
	private static BatchDeleteProductsCall batchdeleteproductsCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static String productActivityStartId = EnvSetup.getData("productActivityStartId");
	private static String activityLoginName = EnvSetup.getData("AdminLoginUser");
	private static String activityLoginPswd = EnvSetup.getData("AdminLoginPswd");

	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量删除商品");
	}

	@Before
	public void caseUp() {
		batchdeleteproductsCall=new BatchDeleteProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchdeleteproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_001() {
		Logger.start(true,"删除普通商品");
		try {
			//TODO 补全case代码

			//发布一个未立即上架的商品
			String productId1 = addNewProductNotOnSale(sellerId, sellerName);
			String productId2 = addNewProductNotOnSale(sellerId, sellerName);
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productId1);
			list.add(productId2);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			
			//断言
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
			batchdeleteproductsCall.truever_step(productId1,0);
			batchdeleteproductsCall.truever_step(productId2,0);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productId1);
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productId2);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_002() {
		Logger.start(false,"删除已上架商品（无法删除）");
		try {
			//TODO 补全case代码

			//发布一个已上架商品
			String productID = addNewProductOnSale(sellerId, sellerName);
			//查询商品名称
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productID);
			String productName =  ymtProductsInte.get(0).getsProduct();
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
			batchdeleteproductsCall.truever_step(productID,0);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productID);
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@Ignore
	@TestCase
	public void Tc_BatchDeleteProducts_003() {
		Logger.start(false,"删除未开始直播的商品（无法删除）");
		try {
			//TODO 补全case代码

			//创建一个即将开始的直播
			int activityId =createLiveInFuture(sellerId, sellerName);			
			//发布未上架商品
			String productID = addNewProductNotOnSale( sellerId,  sellerName);
			//查询商品名称
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productID);
			String productName =  ymtProductsInte.get(0).getsProduct();
			
			//将商品添加到直播
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			String cantDeleteProductsNames = (String) result.getJSONObject("Data").getJSONArray("CantDeleteProductsNames").get(0);
			Logger.verifyEquals(productName, cantDeleteProductsNames, "无法删除的商品名进行对比");
			//SQL、Mango断言
			batchdeleteproductsCall.cantdeletever_step(productID,0,activityId); 	//status为2 无效状态，当前不可售
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_BatchDeleteProducts_004() {
		Logger.start(false,"删除已开始直播的商品（无法删除）");
		try {
			//TODO 补全case代码

			//创建一个即将开始的直播
			int activityId =createLiveing(sellerId, sellerName);
			//发布未上架商品
			String productID = addNewProductNotOnSale( sellerId,  sellerName);
			//查询商品名称
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productID);
			String productName =  ymtProductsInte.get(0).getsProduct();
			
			//将商品添加到直播
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			String cantDeleteProductsNames = (String) result.getJSONObject("Data").getJSONArray("CantDeleteProductsNames").get(0);
			Logger.verifyEquals(productName, cantDeleteProductsNames, "无法删除的商品名进行对比");
			//SQL、Mango断言
			batchdeleteproductsCall.cantdeletever_step(productID,0,activityId); 	
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_005() {
		Logger.start(true,"删除已结束直播的商品");
		try {
			//TODO 补全case代码
			
			//创建一个即将开始的直播
			int activityId =createLiveing(sellerId, sellerName);
			//发布未上架商品
			String productID = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");

			//将商品添加到直播
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
			
			//结束直播
			LiveCallService.closeLive(activityId, sellerId);
			
			//更新直播结束时间(服务器时间不同步)
			YmtProductsInLiveWapper ymtProductsInLiveWapper  = new YmtProductsInLiveWapper();
			Date upDateEndtime = YMTDateUtil.getDateBeforeOrNextSecond(-10);
			ymtProductsInLiveWapper.updateEndtimeByProductId(upDateEndtime, productID);		
			//更新Mongo时间	
			new LiveProductModelWapper().update(MapUtil.hashMap("LiveId", activityId), MapUtil.hashMap("EndTime", upDateEndtime));
			
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			
			//报文断言
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
							
			//SQL、Mango断言
			batchdeleteproductsCall.truever_step(productID,activityId);  //应成功删除商品
			

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_006() {
		Logger.start(false,"删除活动中的商品（无法删除）");
		try {
			//TODO 补全case代码

			//发布商品并申请参加活动
			String productID = SellerProductCallService.addNewProductAndApproveActivity(sellerId, sellerName, activityLoginName, activityLoginPswd, productActivityStartId, null);
			
			//查询商品名称
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productID);
			String productName =  ymtProductsInte.get(0).getsProduct();
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			String cantDeleteProductsNames = (String) result.getJSONObject("Data").getJSONArray("CantDeleteProductsNames").get(0);
			Logger.verifyEquals(productName, cantDeleteProductsNames, "无法删除的商品名进行对比");
			//SQL、Mango断言
			batchdeleteproductsCall.cantdeletever_step(productID,0,0);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_007() {
		Logger.start(true,"删除活动结束商品");
		try {
			//TODO 补全case代码

			//发布商品并申请参加活动
			String productID = SellerProductCallService.addNewProductAndApproveActivity(sellerId, sellerName, activityLoginName, activityLoginPswd, productActivityStartId, null);

			
			//更新活动商品为失效
			YmtProductsInActivityWapper yp = new YmtProductsInActivityWapper();
			yp.updateiStatus(6, productID);
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productID);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			//报文断言
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
			//SQL、Mango断言
			batchdeleteproductsCall.truever_step(productID,0);  //应成功删除商品			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_009() {
		Logger.start(false,"非法的用户id");
		try {
			//TODO 补全case代码
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add("46e73f8a-9031-4c39-8e3d-fae500ce7fcd");
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",0);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			batchdeleteproductsCall.batchDeleteProductsreturnerrcode(result, 400, 0, "非法的用户Id");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_010() {
		Logger.start(false,"商品Id列表不能为空");
		try {
			//TODO 补全case代码
			
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add("46e73f8a-9031-4c39-8e3d-fae500ce7fcd");
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",0);
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			batchdeleteproductsCall.batchDeleteProductsreturnerrcode(result, 400, 0, "商品Id列表不能为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_011() {
		Logger.start(true,"删除已上架售罄商品(无法删除)");
		try {
			//TODO 补全case代码

			//发布一个未立即上架的商品
			String productId1 = addNewProductOnSale(sellerId, sellerName);
			
			YMTDateUtil.waitTime(0.5);
			
			//更新库存为0
			//updatestocknum(0, productId1);
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId1);
			
			YMTDateUtil.waitTime(0.5);
						
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productId1);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);
			
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
			batchdeleteproductsCall.truever_step(productId1,0);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productId1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_012() {
		Logger.start(true,"删除未上架售罄商品");
		try {
			//TODO 补全case代码

			//发布一个未立即上架的商品
			String productId1 = addNewProductNotOnSale(sellerId, sellerName);
			
			YMTDateUtil.waitTime(0.5);
			
			//更新库存为0
			updatestocknum(0, productId1);
			
			YMTDateUtil.waitTime(0.5);
						
			//批量删除商品
			List<String> list=new ArrayList<>();
			list.add(productId1);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = batchdeleteproductsCall.callAndGetReturnData(calldate);			
			
			//断言
			batchdeleteproductsCall.batchDeleteProductsreturncode(result);
			batchdeleteproductsCall.truever_step(productId1,0);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productId1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
	

	
	
	/**
	 * 创建一个正在执行的直播
	 * @return 直播id
	 * @throws Exception
	 */
	@SuppressWarnings("null")
	public int createLiveing(int sellerId, String sellerName) throws Exception {
		try {
			//发布商品
			JSONObject createLiveInProgressObject = LiveCallService.createLiveInProgress(sellerName, sellerId);
			Logger.verifyEquals(createLiveInProgressObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			int activityId = createLiveInProgressObject.getJSONObject("Data").getInt("ActivityId");
			Logger.verifyNotNull(activityId, "响应报文-直播ID");
			return activityId;
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return (Integer) null;
	}
	
	/**
	 * 创建一个即将开始的直播
	 * @return 直播id
	 * @throws Exception
	 */
	@SuppressWarnings("null")
	public int createLiveInFuture(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject createLiveInProgressObject = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals(createLiveInProgressObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			int activityId =  createLiveInProgressObject.getJSONObject("Data").getInt("ActivityId");
			Logger.verifyNotNull(activityId, "响应报文-直播ID");
			return activityId;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return (Integer) null;
	}
	
		
	/**
	 * 发布一个立即上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个未上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductNotOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	
	/**
	 * 更新库存
	 * @param iNum 库存数量
	 * @param productID 商品id
	 * @return
	 * @throws Exception
	 */
	public String updatestocknum(int iNum ,String productID) throws Exception {
	    try {	    	
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			ymtCatalogsIWapper.updatestocknumByCatalogId(iNum , productID);
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 直播中商品开始销售
	 * @param sellerId
	 * @param activityId
	 * @param productID
	 * @throws Exception
	 */
	public void productStartSale(int sellerId, int activityId, String productID) throws Exception {
		try{
			ActivityProductSaleBean activityproductsaleBean = new ActivityProductSaleBean();
			ActivityProductStartSaleCall activityproductstartsaleCall = new ActivityProductStartSaleCall();
			
			activityproductsaleBean.setActivityId(activityId);
			activityproductsaleBean.setUserId(sellerId);
			List<String> productIds = new ArrayList<>();
			productIds.add(productID);
			activityproductsaleBean.setProductIds(productIds);			
			activityproductstartsaleCall.setData(activityproductsaleBean);			
			activityproductstartsaleCall.callService();
			
			//获取返回报文的数据
			String dataStr = activityproductstartsaleCall.getReturnData();
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");
			//断言"Code":200
			String code =  new JSONObject(dataStr).getString("Code");
			Logger.verifyIsContains(code, 200, "\"Code\":200");
			//断言"Msg":""
			String msg =  new JSONObject(dataStr).getString("Msg");
			Logger.verifyIsNull(msg, "Msg等于空");			
			
		}catch(Exception e){
			Logger.fail(e);
		}				
	}		
	

	



	
}